package ops.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ops.controller.ControllerManager;
import ops.controller.StudentController;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.forms.Form;
import ops.view.forms.component.ComboboxInput;
import ops.view.forms.component.LimitedSizeInput;
import ops.view.forms.component.NumberInput;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class StudentRegisterPanel extends JPanel implements Refreshable {

	private StudentController studentController = ControllerManager.getInstance().getStudentController();
	
	private JPanel formPanel;

	private JPanel buttonsPanel;
	private JButton newButton;
	private JButton saveButton;
	private JButton removeButton;
	private JButton cancelButton;

	private TableMaker tableMaker;
	private Form form;
	private ComboboxInput coursesList;
	private ComboboxInput termsList;

	public void clearTable() {
		tableMaker.clearTable();
	}
	
	public void addRowToTable(Object... data) {
		tableMaker.addRow(data);
	}

	private void init() {
		formPanel = new JPanel(new BorderLayout());

		initButtons();
		initForm();
		initTable();

		add(formPanel, BorderLayout.SOUTH);
	}
	
	public StudentRegisterPanel() {
		super(new BorderLayout());
		
		init();
	}

	private void initForm() {
		Resources resources = Resources.getInstance();
		
		form = new Form();
		
		coursesList = new ComboboxInput(resources.getString("course")+":", true, studentController.getCoursesList());
		termsList = new ComboboxInput(resources.getString("admissionTerm"), true, ControllerManager.getInstance().getTermController().getTermsList());

		form.addInput("student.code", new NumberInput(resources.getString("code")+":", 20, true, false));
		form.addInput("student.name", new LimitedSizeInput(resources.getString("name")+":", 40, true));
		form.addInput("student.course", coursesList);
		form.addInput("student.email", new LimitedSizeInput(resources.getString("email")+":", 40, true));
		form.addInput("student.term", termsList);
		
		formPanel.add(form.getPanel(), BorderLayout.SOUTH);
		
	}
	private void initTable() {
		Resources resources = Resources.getInstance();
		
		tableMaker = new TableMaker();
		tableMaker.addColumn(new Column(resources.getString("code"), Long.class, 15));
		tableMaker.addColumn(new Column(resources.getString("name"), String.class, 70));
		tableMaker.addColumn(new Column(resources.getString("course"), String.class));
		tableMaker.addColumn(new Column(resources.getString("email"), String.class));
		tableMaker.addColumn(new Column(resources.getString("admissionTerm"), String.class));
		
		tableMaker.setControlButtons(saveButton, removeButton);

		tableMaker.addSelectionListener(new SelectionListener());
		
		add(ViewHelper.makeScrollPane(tableMaker.getTable()), BorderLayout.CENTER);
		
	}
	
	private void initButtons() {
		buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));

		ButtonListener listener = new ButtonListener();

		removeButton = ViewHelper.makeRemoveButton();
		removeButton.addActionListener(listener);
		saveButton = ViewHelper.makeSaveButton();
		saveButton.addActionListener(listener);
		newButton = ViewHelper.makeNewButton();
		newButton.addActionListener(listener);
		cancelButton = ViewHelper.makeCancelButton();
		cancelButton.addActionListener(listener);

		buttonsPanel.add(newButton);
		buttonsPanel.add(saveButton);
		buttonsPanel.add(removeButton);
		buttonsPanel.add(cancelButton);

		formPanel.add(buttonsPanel, BorderLayout.CENTER);
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (!(e.getSource() instanceof JButton)) {
				return;
			}
			
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}

			Long code = form.getLongFor("student.code");
			String name = form.getStringFor("student.name");
			int course = form.getIntegerFor("student.course");
			String email = form.getStringFor("student.email");
			int admission = form.getIntegerFor("student.term");
			
			if (button == saveButton) {
				if (form.validate()) {				
					studentController.saveStudent(code, name, course, email, admission);
					
					saveButton.setEnabled(false);
					newButton.setEnabled(true);
					
					form.clear();
					studentController.endAddProcess();
				} else {
					ViewHelper.showFormValidationError();
					return;
				}
			} else if (button == removeButton) {
				studentController.removeStudent(code);
			} else if (button == newButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(true);
				newButton.setEnabled(false);
				
				studentController.startAddProcess();
			} else if (button == cancelButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(false);
				newButton.setEnabled(true);
				
				studentController.endAddProcess();
			}
		}
	}
	
	private class SelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			
			ListSelectionModel lsm = (ListSelectionModel) e.getSource();

			if (!lsm.isSelectionEmpty()) {
				Long code = (Long) tableMaker.getValueAt(tableMaker.getSeletedRow(), 0);
				String name = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 1);
				String course = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 2);
				String email = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 3);
				String admission = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 4);
				
				form.setValue("student.code", code);
				form.setValue("student.name", name);
				form.setValue("student.course", course);
				form.setValue("student.email", email);
				form.setValue("student.term", admission);
			} else {
				form.clear();
			}
		}		
	}

	public void refresh() {
		studentController.updateTable();
		coursesList.updateData(studentController.getCoursesList());
		termsList.updateData(ControllerManager.getInstance().getTermController().getTermsList());
	}
}
